// base
import Taro, { Component } from '@tarojs/taro'
import { View, Image } from '@tarojs/components'
import { connect } from '@tarojs/redux'

// less
import './index.less'
import { navigateTo } from '../../../../crossplatform/apiservice/navigate'
import pagejumplist from '../../../../configuration/pagejumplist.json'
import { avaterCamerar, avater } from '../../../../configuration/staticimg'
import { showToast } from '../../../../crossplatform/apiservice/toast'
import { UPDATEUSERINFO } from '../../../../store/types/userinfo'
import { getZhichuUserInfo } from '../../../../store/actions/userinfo'
import { UPDATEPAGESELFCENTER } from '../../store/types/pageselfcenter'
import scaleImageSrc from '../../../../util/pagemethods/scaleImageSrc'
import { judgeLoginStatus } from '../../../../util/login_status'

const mapActiontoProps = dispatch => ({
  updateSelfCenter(payload) {
    dispatch({
      type: UPDATEPAGESELFCENTER,
      payload: {
        ...payload
      }
    })
  },
  updateState(payload) {
    dispatch({
      type: UPDATEUSERINFO,
      payload: {
        ...payload
      }
    })
  },
  updateUserState(payload) {
    dispatch({
      type: UPDATEUSERINFO,
      payload: {
        ...payload
      }
    })
  },
  getZhichuUserInfo(payload) {
    return new Promise(resolve => {
      let promise = getZhichuUserInfo(payload)
      dispatch(promise)
      promise.payload.then(data => {
        resolve(data)
      })
    })
  }
})
@connect(
  ({ userinfo }) => ({
    ...userinfo
  }),
  mapActiontoProps
)
class MyMessage extends Component {
  componentWillMount() {
    // onLoad
  }

  componentDidMount() {
    // onready
  }

  componentDidShow() {
    // onshow
  }

  goLogin = () => {
    this.props.onGoLogin()
  }

  goUploadAvator = () => {
    this.props.onUploadAvator()
  }

  gotoresume() {
    if (this.props.zhichuUserId) {
      navigateTo({
        url: pagejumplist['my-resume'].path
      })
    } else {
    }
  }

  gocollections() {
    judgeLoginStatus({ url: 'job-collection' })
  }

  getName = () => {
    const { userInfoB, userInfoC, role, userId } = this.props
    
    if (role == 'company') {
      return userInfoB.userName || (userId ? '完善信息' : '登录')
    } else {
      return userInfoC.userName || (userId ? '完善信息' : '登录')
    }
  }

  noOpen = () => {
    showToast({
      title: '功能暂未开启'
    })
  }

  getuserAvator = () => {
    const { userInfoB, userInfoC, role, companyId } = this.props
    if (role == 'company' && companyId) {
      let userAvator = userInfoB.userAvator
      return {
        userAvator: userAvator
      }
    } else {
      let userAvator = userInfoC.userAvator
      return {
        userAvator: userAvator
      }
    }
  }

  gotoSetting = () => {
    navigateTo({
      url: pagejumplist['setting-page'].path
    })
  }

  render() {
    const { collectCount, userId, userInfoC } = this.props
    const userformat = this.getuserAvator()
    return (
      <View className="MyMessage_id94118">
        <View className="mymessagewrap">
          <View className="userAvator-wrap">
            <Image
              className="mymessageright"
              src={
                scaleImageSrc(userformat.userAvator) ||
                (userId ? avater(userInfoC.gender, userId) : avaterCamerar)
              }
              mode="aspectFill"
              onClick={this.goUploadAvator}
              lazy-load
            />
            <View className="name" onClick={this.goLogin}>
              {this.getName()}
            </View>
          </View>
          <View className="setting" onClick={this.gotoSetting}>
            <Image
              className="setting-img"
              src="https://imagesize.hrwork.com/sp/files/1aea35da-efe1-4ab0-b3ee-ec5517534ae9.png"
              lazy-load
            />
          </View>
        </View>

        <View className="messagelist">
          <View className="mslist" onClick={this.gocollections}>
            <View className="text">职位收藏</View>
            <View className="num">{collectCount || 0}</View>
            <Image
              className="icon"
              src="https://imagesize.hrwork.com/sp/files/059380d3-a914-489c-9e36-89a298ce9fa2.png"
            />
          </View>
          <View className="mslist" onClick={this.noOpen}>
            <View className="text">沟通历史</View>
            <View className="num">0</View>
            <Image
              className="icon"
              src="https://imagesize.hrwork.com/sp/files/2bf48289-d90a-4335-98cb-512ab39cfafb.png"
            />
          </View>
        </View>
      </View>
    )
  }
}

MyMessage.defaultProps = {
  userInfoC: {},
  viewCounts: 0,
  shareCounts: 0
}

export default MyMessage
